<?xml version="1.0"?>
<!--
    Transform an XML Validation Report into presentable HTML.
    Author: Maksim Bezrukov
    Version: 1.0
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" />
    <!-- Parameter to select a full HTML document (true) or a fragment within div (false) -->
    <xsl:param name="isFullHTML" select="'true'" />
    <!-- Parameter to show parser type -->
    <xsl:param name="parserType"/>
    <!-- Parameter to set the base path to the Wiki instance -->
    <xsl:param name="wikiPath" select="'https://github.com/veraPDF/veraPDF-validation-profiles/wiki/'"/>
    <xsl:strip-space elements="*"/>

    <!-- HTML header and body wrapper -->
    <xsl:template match="/">
      <xsl:choose>
        <!-- If we're full HTML then wrap the div in an HTML document -->
        <xsl:when test="$isFullHTML='true'">
          <html>
              <head>
                  <title>Validation Report</title>
              </head>
              <body>
                  <img alt="veraPDF Logo"
                       src=""/>
                  <style>
                    body {
                       font-family: sans-serif;
                    }
                  </style>
                  <h1 align="left">
                      <strong>
                          <b>Validation Report</b>
                      </strong>
                  </h1>
                  <xsl:apply-templates/>
              </body>
          </html>
        </xsl:when>
        <!-- We're  NOT full HTML so just output the div -->
        <xsl:otherwise>
          <xsl:apply-templates/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

    <!-- Validation Report header -->
    <xsl:template match="report">
        <div>
            <!-- A little bit of style information -->
            <style>
              table {
                border-collapse: collapse;
                width: 100%;
              }
              th {
                height: 50px;
              }
              th, td {
                padding: 15px;
              }
              table, th, td {
                border: 1px solid black;
                text-align: center;
              }
              tr:nth-child(even) {
                background-color: #f2f2f2
              }
              .invalid {
                color: red;
                font-weight: bold;
              }
              .valid {
                color: green;
                font-weight: bold;
              }
              .lefted {
                text-align: left;
              }
            </style>
            <!-- These scripts used to hide and show the hidden details -->
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
            <script type="text/javascript">
                if(typeof $ === 'function'){
                $(document).ready(function(){
                $(".hideable").hide();
                $(".hide-tr").show();
                $(".hide-tr").click(function(){
                $("." + $(this).attr("data-target")).toggle();
                var prevText = $(this).text();
                $(this).text($(this).attr('data-translation-toggle'));
                $(this).attr('data-translation-toggle', prevText)
                return false;
                });
                });
                }
            </script>
          </div>
          <!-- Display the build information -->
          <xsl:apply-templates select="/report/buildInformation"/>
          <!-- Display the summary information -->
          <xsl:apply-templates select="/report/batchSummary"/>
          <!-- Call the job iteration template -->
          <xsl:apply-templates select="/report/jobs"/>
    </xsl:template>

    <!-- Display the build information -->
    <xsl:template match="/report/buildInformation">
      <h2>Build Information</h2>
      <table border="0" id="table2">
          <tr>
              <td class="lefted" width="250">
                  <b>Version:</b>
              </td>
              <td class="lefted">
                  <xsl:value-of select="releaseDetails[@id='gui']/@version"/>
              </td>
          </tr>
          <xsl:if test="$parserType">
              <tr>
                  <td class="lefted" width="250">
                      <b>Parser:</b>
                  </td>
                  <td class="lefted">
                      <xsl:value-of select="$parserType"/>
                  </td>
              </tr>
          </xsl:if>
          <tr>
              <td class="lefted" width="250">
                  <b>Build Date:</b>
              </td>
              <td class="lefted">
                  <xsl:value-of select="releaseDetails[@id='gui']/@buildDate"/>
              </td>
          </tr>
        </table>
    </xsl:template>

    <!-- Display the summary information -->
    <xsl:template match="/report/batchSummary">
      <xsl:variable name="isPolicy" select="/report/jobs/job/policyReport" />
      <h2>Batch Summary</h2>
      <table border="0" id="table2">
          <tr>
            <th>Processing time</th>
            <th>Total Jobs</th>
            <th>Failed to Parse</th>
            <th>Encrypted</th>
            <th>PDF/A Compliant</th>
            <th>Not PDF/A Compliant</th>
            <xsl:if test="$isPolicy">
              <th>Policy Compliant</th>
              <th>Not Policy Compliant</th>
            </xsl:if>
          </tr>
          <tr>
              <td>
                  <xsl:value-of select="duration/text()"/>
              </td>
              <td>
                  <xsl:value-of select="@totalJobs"/>
              </td>
              <td>
                  <xsl:value-of select="@failedToParse"/>
              </td>
              <td>
                  <xsl:value-of select="@encrypted"/>
              </td>
              <td>
                  <xsl:value-of select="validationReports/@compliant"/>
              </td>
              <td>
                  <xsl:value-of select="validationReports/@nonCompliant"/>
              </td>
              <xsl:if test="$isPolicy">
                <td>
                  <xsl:value-of select="count(/report/jobs/job/policyReport[@failedChecks = 0])"/>
                </td>
                <td>
                  <xsl:value-of select="count(/report/jobs/job/policyReport[@failedChecks > 0])"/>
                </td>
              </xsl:if>
          </tr>
      </table>
    </xsl:template>

    <!-- Iterate the job reports and output the details -->
    <xsl:template match="/report/jobs">
      <xsl:variable name="isPolicy" select="/report/jobs/job/policyReport" />
      <h2>Job Summary</h2>
      <table>
        <tr>
          <th>File Name</th>
          <th>Validation Profile</th>
          <th>PDF/A Compliance</th>
          <th>Passed Rules</th>
          <th>Failed Rules</th>
          <th>Passed Checks</th>
          <th>Failed Checks</th>
          <xsl:if test="$isPolicy">
            <th>Policy Check</th>
          </xsl:if>
          <th>Duration</th>
        </tr>
        <xsl:for-each select="job">
          <xsl:variable name="validClass">
            <xsl:choose>
              <xsl:when test="validationReport/@isCompliant = 'true'">
                <xsl:value-of select="'valid'" />
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="'invalid'" />
                </xsl:otherwise>
              </xsl:choose>
          </xsl:variable>
          <xsl:variable name="validResult">
            <xsl:choose>
              <xsl:when test="validationReport/@isCompliant = 'true'">
                <xsl:value-of select="'Passed'" />
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="'Failed'" />
                </xsl:otherwise>
              </xsl:choose>
          </xsl:variable>
          <tr>
            <td class="lefted">
              <xsl:value-of select="item/name" />
            </td>
            <td>
              <xsl:value-of select="validationReport/@profileName" />
            </td>
            <td class="{$validClass}">
                <xsl:value-of select="$validResult"/>
            </td>
            <td>
              <xsl:value-of select="validationReport/details/@passedRules" />
            </td>
            <td>
              <xsl:value-of select="validationReport/details/@failedRules" />
            </td>
            <td>
              <xsl:value-of select="validationReport/details/@passedChecks" />
            </td>
            <td>
              <xsl:value-of select="validationReport/details/@failedChecks" />
            </td>
            <xsl:apply-templates select="policyReport" />
            <td>
              <xsl:value-of select="duration/text()" />
            </td>
          </tr>
        </xsl:for-each>
      </table>
    </xsl:template>

    <xsl:template match="/report/jobs/job/policyReport">
      <xsl:variable name="policyClass">
        <xsl:choose>
          <xsl:when test="@failedChecks > 0">
            <xsl:value-of select="'invalid'" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="'valid'" />
            </xsl:otherwise>
          </xsl:choose>
      </xsl:variable>
      <xsl:variable name="policyResult">
        <xsl:choose>
          <xsl:when test="@failedChecks > 0">
            <xsl:value-of select="'Failed'" />
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="'Passed'" />
            </xsl:otherwise>
          </xsl:choose>
      </xsl:variable>
      <td class="{$policyClass}">
          <xsl:value-of select="$policyResult"/>
      </td>
    </xsl:template>

</xsl:stylesheet>
